package org.dromara.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.Audit;
import org.dromara.business.domain.bo.AuditBo;
import org.dromara.business.domain.vo.AuditVo;
import org.dromara.business.mapper.AuditMapper;
import org.dromara.business.service.AuditService;
import org.dromara.business.service.WalletChangeService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 提现审核Service业务层处理
 *
 * @author zlj
 * @date 2024-08-22
 */
@RequiredArgsConstructor
//@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    private final AuditMapper baseMapper;

    private final WalletChangeService walletChangeService;

    /**
     * 查询提现审核
     */
    @Override
    public AuditVo queryById(Long auditId) {
        return baseMapper.selectVoById(auditId);
    }

    /**
     * 查询提现审核列表
     */
    @Override
    public TableDataInfo<AuditVo> queryPageList(AuditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(bo);
        Page<AuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询提现审核列表
     */
    @Override
    public List<AuditVo> queryList(AuditBo bo) {
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Audit> buildQueryWrapper(AuditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Audit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBalance() != null, Audit::getBalance, bo.getBalance());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), Audit::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getReason()), Audit::getReason, bo.getReason());
        lqw.eq(StrUtil.isNotBlank(bo.getAlipay()), Audit::getAlipay, bo.getAlipay());
        lqw.orderByDesc(Audit::getCreateTime);
        return lqw;
    }

    /**
     * 新增提现审核
     */
    @Override
    public Boolean insertByBo(AuditBo bo) {
        Audit add = MapstructUtils.convert(bo, Audit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAuditId(add.getAuditId());
        }
        return flag;
    }

    /**
     * 修改提现审核
     */
    @Override
    public Boolean updateByBo(AuditBo bo) {
        Audit update = MapstructUtils.convert(bo, Audit.class);
        //validEntityBeforeSave(update);
        if (update.getStatus().equals("2")) {
            Audit audit = baseMapper.selectById(update.getAuditId());
            //拒绝,提现金额返还钱包
            walletChangeService.changeUserAmount(audit.getCreateBy().toString(), update.getBalance(), false, 3);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Audit entity) {

    }

    /**
     * 批量删除提现审核
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<AuditVo> myList(PageQuery pageQuery) {
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(new AuditBo());
        lqw.eq(Audit::getCreateBy, LoginHelper.getUserId());
        Page<AuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
